using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace MyCityCab.Core
{
	/// <summary>
	/// Strongly-typed collection for the Route class.
	/// </summary>
	[Serializable]
	public partial class RouteCollection : ActiveList<Route, RouteCollection> 
	{	   
		public RouteCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Route table.
	/// </summary>
	[Serializable]
	public partial class Route : ActiveRecord<Route>
	{
		#region .ctors and Default Settings
		
		public Route()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Route(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Route(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Route(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Route", TableType.Table, DataService.GetInstance("MyCityCab"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "Id";
				colvarId.DataType = DbType.Int32;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = true;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarFromLocationId = new TableSchema.TableColumn(schema);
				colvarFromLocationId.ColumnName = "FromLocation_Id";
				colvarFromLocationId.DataType = DbType.Int32;
				colvarFromLocationId.MaxLength = 0;
				colvarFromLocationId.AutoIncrement = false;
				colvarFromLocationId.IsNullable = false;
				colvarFromLocationId.IsPrimaryKey = false;
				colvarFromLocationId.IsForeignKey = true;
				colvarFromLocationId.IsReadOnly = false;
				colvarFromLocationId.DefaultSetting = @"";
				
					colvarFromLocationId.ForeignKeyTableName = "Location";
				schema.Columns.Add(colvarFromLocationId);
				
				TableSchema.TableColumn colvarToLocationId = new TableSchema.TableColumn(schema);
				colvarToLocationId.ColumnName = "ToLocation_Id";
				colvarToLocationId.DataType = DbType.Int32;
				colvarToLocationId.MaxLength = 0;
				colvarToLocationId.AutoIncrement = false;
				colvarToLocationId.IsNullable = false;
				colvarToLocationId.IsPrimaryKey = false;
				colvarToLocationId.IsForeignKey = true;
				colvarToLocationId.IsReadOnly = false;
				colvarToLocationId.DefaultSetting = @"";
				
					colvarToLocationId.ForeignKeyTableName = "Location";
				schema.Columns.Add(colvarToLocationId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MyCityCab"].AddSchema("Route",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("Id")]
		public int Id 
		{
			get { return GetColumnValue<int>("Id"); }

			set { SetColumnValue("Id", value); }

		}

		  
		[XmlAttribute("FromLocationId")]
		public int FromLocationId 
		{
			get { return GetColumnValue<int>("FromLocation_Id"); }

			set { SetColumnValue("FromLocation_Id", value); }

		}

		  
		[XmlAttribute("ToLocationId")]
		public int ToLocationId 
		{
			get { return GetColumnValue<int>("ToLocation_Id"); }

			set { SetColumnValue("ToLocation_Id", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public MyCityCab.Core.JourneyCollection JourneyRecords()
		{
			return new MyCityCab.Core.JourneyCollection().Where(Journey.Columns.RouteId, Id).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Location ActiveRecord object related to this Route
		/// 
		/// </summary>
		public MyCityCab.Core.Location Location
		{
			get { return MyCityCab.Core.Location.FetchByID(this.FromLocationId); }

			set { SetColumnValue("FromLocation_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a Location ActiveRecord object related to this Route
		/// 
		/// </summary>
		public MyCityCab.Core.Location LocationToToLocationId
		{
			get { return MyCityCab.Core.Location.FetchByID(this.ToLocationId); }

			set { SetColumnValue("ToLocation_Id", value.Id); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varFromLocationId,int varToLocationId)
		{
			Route item = new Route();
			
			item.FromLocationId = varFromLocationId;
			
			item.ToLocationId = varToLocationId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varId,int varFromLocationId,int varToLocationId)
		{
			Route item = new Route();
			
				item.Id = varId;
				
				item.FromLocationId = varFromLocationId;
				
				item.ToLocationId = varToLocationId;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"Id";
			 public static string FromLocationId = @"FromLocation_Id";
			 public static string ToLocationId = @"ToLocation_Id";
						
		}

		#endregion
	}

}

